package com.jms.controller.cms;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jms.controller.base.BaseController;
import com.jms.dao.BaseDaoI;
import com.jms.framework.constant.GlobalConstant;
import com.jms.model.cms.Tcate;
import com.jms.pageModel.base.Grid;
import com.jms.pageModel.base.Json;
import com.jms.pageModel.base.PageFilter;
import com.jms.pageModel.base.SessionInfo;
import com.jms.pageModel.cms.News;
import com.jms.service.cms.CateServiceI;
import com.jms.service.cms.NewsServiceI;

@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {

	@Autowired
	private NewsServiceI newsService;
	@Autowired
	private BaseDaoI<Tcate> cateDao;
	
	
	private Integer pageSize;
	private String newsSort;
	private String newsOrder;
	public Integer getPageSize() {
		return pageSize;
	}
	@Value("#{frontSettings['default_pagesize']}")
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public String getNewsSort() {
		return newsSort;
	}
	@Value("#{frontSettings['news_sort']}")
	public void setNewsSort(String newsSort) {
		this.newsSort = newsSort;
	}
	public String getNewsOrder() {
		return newsOrder;
	}
	@Value("#{frontSettings['news_order']}")
	public void setNewsOrder(String newsOrder) {
		this.newsOrder = newsOrder;
	}
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		return "/admin/news";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(News news, PageFilter ph) {
		Grid grid = new Grid();
		if(news.getStatus()==null){
			news.setStatus(0);
		}
		grid.setRows(newsService.dataGrid(news, ph));
		grid.setTotal(newsService.count(news, ph));
		return grid;
	}
	@RequestMapping("/list")
	public String list(HttpServletRequest request, Long cid, PageFilter ph) {
		ph.setRows(pageSize);
		ph.setOrder(newsOrder);
		ph.setSort(newsSort);
		News news = new News();
		if(cid!=null&&!"".equals(cid)){
			news.setCategoryId(cid);
		}
		List<News> newslist = newsService.dataGrid(news, ph);
		Long record = newsService.count(news, ph);
		Long total=0l;
		if(record!=null&&record!=0l){
			total = record/pageSize+1;
		}
		int page = ph.getPage();
		if(page == 0){page=1;}
		request.setAttribute("newslist", newslist);
		request.setAttribute("page",page);
		request.setAttribute("total", total);
		request.setAttribute("cid", cid);
		return "/news/defaultlist";
	}
	@RequestMapping("/detail")
	public String detailNews(HttpServletRequest request, Long id) {
		News news = newsService.get(id);
		request.setAttribute("news", news);
		return "/news/defaultcontent";
	}
	
	@RequestMapping("/addNews")
	public String editPwdPage(HttpServletRequest request) {
		return "/admin/newsAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public String add(HttpServletRequest request,News news) {
		String str = "";
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		
		news.setPublishedbyid(sessionInfo.getId());
		news.setPublishedByName(sessionInfo.getName());
		news.setPubtime(new Date());
			try {
				newsService.add(news);
				str="保存成功";
			} catch (Exception e) {
			}

		return str;
	}
	
	@RequestMapping("/recycle")
	@ResponseBody
	public Json recycle(String ids) {
		Json j = new Json();
		try {
			if(ids!=null&&"".endsWith("")){
				String[] strs = ids.split("\\|");
				for(int i=0;i<strs.length;i++){
					Long id= Long.valueOf(strs[i]);
					newsService.recycle(id);
				}
				j.setMsg("移到垃圾箱成功！");
				j.setSuccess(true);
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String ids) {
		Json j = new Json();
		try {
			if(ids!=null&&!"".endsWith(ids)){
				
				String[] strs = ids.split("\\|");
				for(int i=0;i<strs.length;i++){
					Long id= Long.valueOf(strs[i]);
					newsService.delete(id);
				}
				j.setMsg("删除成功！");
				j.setSuccess(true);
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	@RequestMapping("/movePage")
	public String addPage(HttpServletRequest request,String ids) {
		request.setAttribute("ids", ids);
		return "/admin/newsMove";
	}
	@RequestMapping("/move")
	@ResponseBody
	public Json move(String ids,String categoryId) {
		Json j = new Json();
		try {
			if(ids!=null&&!"".endsWith(ids)&&categoryId!=null){
				
				String[] strs = ids.split("\\|");
				for(int i=0;i<strs.length;i++){
					Long id= Long.valueOf(strs[i]);
					newsService.move(id,Long.valueOf(categoryId));
				}
			}
			j.setMsg("移动成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	@RequestMapping("/editNews")
	public String editPage(HttpServletRequest request, Long id) {
		News news = newsService.get(id);
		request.setAttribute("news", news);
		return "/admin/newsEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(HttpServletRequest request, News news) {
		String str = "";
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		
		news.setModifiedbyid(sessionInfo.getId());
		news.setModifiedByName(sessionInfo.getName());
		news.setModtime(new Date());
			try {
				newsService.edit(news);
				str="修改成功";
			} catch (Exception e) {
			}

		return str;
	}

}
